package com.atwy.storage.service.Impl;

import com.atwy.storage.mapper.StorageMapper;
import com.atwy.storage.model.StorageDo;
import com.atwy.storage.service.StorageService;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class StorageServiceImpl implements StorageService {
    private static final Logger log = LoggerFactory.getLogger(StorageServiceImpl.class);
    @Autowired
    StorageMapper storageMapper;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean prepare(BusinessActionContext businessActionContext, String commodityCode, int count) {
        StorageDo storageDo = storageMapper.queryStorageByCommodityCode(commodityCode);
        if (storageDo == null) {
            throw new RuntimeException("商品不存在");
        }
        if (storageDo.getCount() < count) {
            throw new RuntimeException("库存不足,预扣库存失败");
        }
        storageDo.setCount(storageDo.getCount() - count);
        storageDo.setFreezeCount(storageDo.getFreezeCount() + count);
        int i = storageMapper.updateStorage(storageDo);
        log.info("{}预扣库存，{}", commodityCode, count);
        return i == 1;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean commit(BusinessActionContext businessActionContext) {
        String commodityCode = (String) businessActionContext.getActionContext("commodityCode");
        Integer count = (Integer) businessActionContext.getActionContext("count");
        StorageDo storageDo = storageMapper.queryStorageByCommodityCode(commodityCode);
        if (storageDo.getFreezeCount() < count) {
            throw new RuntimeException("库存不足，扣库存失败");
        }
        storageDo.setFreezeCount(storageDo.getFreezeCount() - count);
        int i = storageMapper.updateStorage(storageDo);
        log.info("库存处理成功,commodityCode:{},orderCount:{}", commodityCode, count);
        return i == 1;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean rollback(BusinessActionContext businessActionContext) {
        String commodityCode = (String) businessActionContext.getActionContext("commodityCode");
        Integer count = (Integer) businessActionContext.getActionContext("count");
        StorageDo storageDo = storageMapper.queryStorageByCommodityCode(commodityCode);
        if (storageDo.getFreezeCount() >= count) {
            storageDo.setFreezeCount(storageDo.getFreezeCount() - count);
            storageDo.setCount(storageDo.getCount() + count);
            int i = storageMapper.updateStorage(storageDo);
            log.info("库存处理失败,回滚成功,commodityCode:{},orderCount:{}", commodityCode, count);
            return i == 1;
        }
        log.info("库存处理失败,已释放库存,commodityCode:{}", commodityCode);
        return true;
    }
}
